package com.cby.operation.initialize

import android.app.Activity
import com.cby.operation.common.util.NetworkUtils
import com.cby.operation.initialize.data.InitApiRepository
import com.cby.operation.initialize.data.InitConfig
import com.cby.operation.sdk.*
import com.cby.operation.common.ext.showToast
import com.cby.operation.initialize.ui.MaintenanceDialogFragment
import java.util.concurrent.atomic.AtomicBoolean

internal class InitManager private constructor(){

    /**
     * 是否初始化成功
     */
    private val isInitSucceed = AtomicBoolean(false)
    var initConfig: InitConfig? = null
    private var activity: Activity? = null
    private var listener: InitListener? = null

    private object SingletonHolder {
        /**
         * 返回InitManager对象
         */
        val instance = InitManager()
    }


    companion object{
        val instance = SingletonHolder.instance
    }

    fun initConfig(
        context: Activity?,
        gameId: String,
        orientation: Int,
        listener: InitListener?
    ){
        activity = context
        this.listener = listener

        //检查参数是否为空
        if (activity == null && listener != null) {
            listener.onInitFailed(
                ERR_CODE_ACTIVITY_IS_NULL,
                ERR_MSG_SDK_ACTIVITY_IS_NULL
            )
            return
        }

        //检查网络是否异常
        if (!NetworkUtils.isConnected(activity!!.applicationContext)) {
            activity?.showToast(ERR_MSG_NETWORK_ERROR)
            listener?.onInitFailed(
                ERR_CODE_NETWORK_ERROR,
                ERR_MSG_NETWORK_ERROR
            )
            return
        }

        //检查是否已经初始化过了，防尘重复初始化
        if (isInitSucceed.get()) {
            //初始化成功后，限制重复初始化
            listener?.onInitSuccess()
            return
        }

        //保存sdk需要的参数信息
        SdkConfiguration.init(context!!, gameId, orientation)

        //开始网络请求之类的逻辑
        InitApiRepository.initConfig(gameId,
            object : InitApiRepository.OnInitApiCallback {
                override fun onInitSucceed(config: InitConfig?) {
                    //初始化配置
                    initConfig = config
                    //标志值设置为true
                    isInitSucceed.set(true)
                    handleInitSuccess()
                }

                override fun onInitFailed(code: Int, msg: String?) {
                    isInitSucceed.set(false)
                    //返回初始化错误信息给游戏
                    handleInitFailedCallBack(code, msg)
                }

                override fun onInitMaintenance(config: InitConfig?) {
                    isInitSucceed.set(false)
                    //显示维护弹窗
                    config!!.maintainContent?.let { MaintenanceDialogFragment.show(activity!!, it) }

                }
            })
    }

    /**
     * 处理初始化失败的回调
     *
     * @param code Int
     * @param msg String?
     */
    fun handleInitFailedCallBack(code: Int, msg: String?) {
        listener!!.onInitFailed(code, msg)
    }

    /**
     * 处理初始化成功的回调
     */
    fun handleInitSuccess() {
        listener!!.onInitSuccess()
    }

    /**
     * 用来判断初始化是否成功了
     *
     * @return Boolean
     */
    fun isInitOK() : Boolean {
        return isInitSucceed.get()
    }

    /**
     * 用来对外给用户在销毁的时候，释放资源
     */
    fun release() {
        listener = null
        activity = null
    }
}